﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Cnm.Models;
using Cnm.Models.ViewModels;
using Cnm.Servicers.Localization;
using Cnm.Servicers.Seo;

namespace Cnm.Mvc.Web.Controllers
{
    public class ProductController : Controller
    {
        private readonly CnmDemoEntities dbContext = new CnmDemoEntities();

        public ActionResult AllProducts()
        {
            var products = dbContext.Products.ToList();
            return PartialView(PrepareProductOverviewModels(products));
        }

        public ActionResult ProductDetails(int productId)
        {
            return View();
        }

        [NonAction]
        protected virtual IEnumerable<ProductOverviewModel> PrepareProductOverviewModels(IEnumerable<Product> products)
        {
            if (products == null)
                throw new ArgumentNullException("products");

            var models = new List<ProductOverviewModel>();
            foreach (var product in products)
            {
                var model = new ProductOverviewModel()
                {
                    Id = product.Id,
                    Name = product.GetLocalized(x => x.Name),
                    ShortDescription = product.GetLocalized(x => x.ShortDescription),
                    FullDescription = product.GetLocalized(x => x.FullDescription),
                    SeName = product.GetSeName()
                };

                models.Add(model);
            }

            return models;
        }

	}
}